package com.jwthhealth_pub.old.report.view.adapter;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.report.model.resbody.SportSuggListResp;
import com.jwthhealth_pub.old.report.view.SportSuggestActivity;

import java.util.List;

/**
 * contain nav & content
 * Created by liuzhen on 2017/11/2.
 */

public class SportsuggDayPlanAdapter extends RecyclerView.Adapter {
    private final SportSuggestActivity mActivity;
    private final List<SportSuggListResp.DataBeanX.DayListBean> mData;
    private int mSelStage = -1;
    private int mSelDay = -1;

    public SportsuggDayPlanAdapter(SportSuggestActivity sportSuggestActivity, List<SportSuggListResp.DataBeanX.DayListBean> day_list) {
        this.mActivity = sportSuggestActivity;
        this.mData = day_list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(View.inflate(mActivity, R.layout.item_sport_day_plan, null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;
        SportSuggListResp.DataBeanX.DayListBean dayListBean = mData.get(vh.getAdapterPosition());
        if (dayListBean == null) return;

        if (dayListBean.getTitle() != null)
            vh.title.setText(dayListBean.getTitle());
        if (dayListBean.getData() != null)
            vh.desc.setText(dayListBean.getDay_long());

        if (vh.getAdapterPosition() == mSelStage) {
            vh.content.setVisibility(View.VISIBLE);
        } else
            vh.content.setVisibility(View.GONE);

        List<SportSuggListResp.DataBeanX.DayListBean.DataBean> contentList = dayListBean.getData();
        if (contentList == null || contentList.size() <= 0)
            return;

        //nav
        SportStepNavAdapter sportStepNavAdapter = new SportStepNavAdapter(mActivity, this, contentList, vh.getAdapterPosition(), mSelStage, mSelDay);
        sportStepNavAdapter.setSelDay(mSelDay);
        LinearLayoutManager manager = new LinearLayoutManager(mActivity);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        vh.catalogue.setLayoutManager(manager);
        vh.catalogue.setAdapter(sportStepNavAdapter);

        //content
        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        vh.content.setLayoutManager(layoutManager);
        SportStepContentAdapter sportStepContentAdapter = new SportStepContentAdapter(mActivity, contentList, vh.getAdapterPosition(), mSelStage, mSelDay);
        vh.content.setAdapter(sportStepContentAdapter);

        if (position == getItemCount() - 1) {
            vh.dividualView.setVisibility(View.GONE);
        }
    }

    void changeDayPlan(int selStage, int selday) {
        this.mSelStage = selStage;
        this.mSelDay = selday;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (mData != null && mData.size() > 0)
            return mData.size();
        else
            return 0;
    }

    private class ViewHolder extends RecyclerView.ViewHolder {

        TextView title, desc;
        RecyclerView catalogue, content;
        View dividualView;

        public ViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            desc = itemView.findViewById(R.id.desc);
            catalogue = itemView.findViewById(R.id.rv_catalogue);
            content = itemView.findViewById(R.id.rv_content);
            dividualView = itemView.findViewById(R.id.view_dividual);
        }
    }
}
